<?php
/**
 * Created by PhpStorm.
 * User: wpf
 * Date: 2018/2/7
 * Time: 15:56
 */

namespace Nuoche\Controller;

use Common\Controller\MemberbaseController;
class CenterController extends MemberbaseController
{
    public function qq(){
        $this->display();
    }
    public function order(){
        $category=I('category','0','trim');//注册状态 0：已注册 1：未注册 2：查看全部
        $plateNum=I('plateNum','','trim');//模糊查询 车牌号
        $tel=I('tel','','trim');//模糊查询 电话号码
        $sortId=I('sortId','','trim');//排序类型 regTime：注册时间 noticeCount：通知次数 lastNoticeTime：最后通知时间
        $sortValue=I('sortValue','','trim');//升降序 asc:升序 desc:降序
        $p=I('p',1);

        $nuoche_chezhu= M('nuoche_chezhu');
             $map=array();
             //注册状态 及 模糊查询车牌号
             if(empty($plateNum)){
                 switch ($category){
                     case 0:
                         $map['plateNum']=array('NEQ','');
                         break;
                     case 1:
                         $map['plateNum']=array('EXP','IS NULL');
                         break;
                 }
         }else{
             switch ($category){
                 case 0:
                 case 2:
                 $map['plateNum']=array('LIKE',"%$plateNum%");
                     break;
                 case 1:
                     $this->assign('qrcodeCategory',$category);
                     $this->assign('plateNum',$plateNum);
                     $this->assign('tel',$tel);
                     $this->assign($sortId,$sortValue);

                     $this->display();
                     break;
             }
         }

        //模糊查询 电话号码
        if (!empty($tel)){
            $map['tel']=array('LIKE',"%$tel%");
        }

        /*$PageParam = C("VAR_PAGE");
        $Page = new \Page($nuoche_chezhu->where($map)->count(),20);
        $Page->setLinkWraper("li");
        $Page->__set("PageParam", $PageParam);
        $Page->SetPager('default', $pagetpl, array("listlong" => "9", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
*/

        //排序
        $listCount=$nuoche_chezhu->where($map)->count();
        $page=$this->page($listCount,20);
        if (empty($sortId)){
            $list= $nuoche_chezhu->page($p,20)->where($map)->select();
        }else{
            if (empty($sortValue)){
                $sortValue='asc';
            }else if($sortValue=='asc'){
                $sortValue='desc';
            }else{
                $sortValue='';
            }
            $sortStr=$sortId.' '.$sortValue;
            $list= $nuoche_chezhu->page($p,20)->where($map)->order($sortStr)->select();
            $this->assign('qrcodeCategory',$category);
            $this->assign('plateNum',$plateNum);
            $this->assign('tel',$tel);
        }

         //$res= $nuoche_chezhu->where($map)->select();

        /*$strSql='SELECT * from cmf_nuoche_chezhu';
        $isFirstWhere=false;
        //二维码注册状态
        switch ($category){
            //已注册
            case 0:
                $strSql.=" WHERE plateNum IS NOT NULL AND plateNum !=''";
                $isFirstWhere=true;
                break;
             //未注册
            case 1:
                $strSql.=" WHERE plateNum IS NULL OR plateNum =''";
                $isFirstWhere=true;
                break;
             //查看全部
            case 2:
                break;
            default:
                break;
        }

        //模糊查询，车牌号
        if (!empty($plateNum)){
            if ($isFirstWhere){
                $strSql.=' AND';
            }else{
                $strSql.=' WHERE';
                $isFirstWhere=true;
            }

            $strSql.=" plateNum like '%$plateNum%'";
        }

        //模糊查询，电话号
        if(!empty($tel)){
            if ($isFirstWhere){
                $strSql.=' AND';
            }else{
                $strSql.=' WHERE';
                $isFirstWhere=true;
            }
            $strSql.=" tel like '%$tel%'";
        }

        if (empty($sortValue)){
            $sortValue='asc';
        }else if($sortValue=='asc'){
            $sortValue='desc';
        }else{
            $sortValue='';
        }

        if (!empty($sortId)&&!empty($sortValue)){
            $strSql.=" ORDER BY $sortId $sortValue;";
        }


        $model=M()->query($strSql);



        $totalsize=count($model);
        import('Page');
        if($pagesize == 0) {
            $pagesize = 20;
        }
        $PageParam = C("VAR_PAGE");
        $page = new \Page($totalsize,$pagesize);
        $page->setLinkWraper("li");
        $page->__set("PageParam", $PageParam);
        $page->SetPager('default', $pagetpl, array("listlong" => "9", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
        //$list = M('nuoche_chezhu')->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();

        $list=array();
        $j=0;
        for ($i=$page->firstRow;$i<$page->firstRow+$page->listRows;$i++){
            if (empty($model[$i])){
                break;
            }

            $list[$j++]=$model[$i];
        }*/

        $this->assign('qrcodeCategory',$category);
        $this->assign('plateNum',$plateNum);
        $this->assign('tel',$tel);
        $this->assign($sortId,$sortValue);
        $this->assign('listCount',$listCount);

        $this->assign('list',$list);
        $this->assign('page', $page->show('default'));
        $this->display();
    }
    public function down_code(){
            $maxid = M('nuoche_chezhu')->max('nid');
            $this->assign('maxid',$maxid);
            $this->display();
    }
    public function export() {
        $start = I('start', 0, 'int');
        $end = I('end', 0, 'int');
        if($end > 0 && $start > 0) {
            header("Content-type:application/octet-stream");
            header("Accept-Ranges:bytes");
            header("Content-Disposition: attachment; filename=码溜二维码" . $start . '-' . $end . '.txt');
            header("Expires:0");
            header("Cache-Control:must-revalidate,post-check=0,pre-check=0 ");
            header("Pragma:public ");
            $list = M('nuoche_chezhu')->where(array('nid' => array('between',array( $start , $end))))->order('nid asc')->select();
            if ($list) {
                foreach ($list as $k => $v) {
                    echo UU('Nuoche/company/index', array('nid' => $v['nid'], 'ncode' => $v['ncode']), true, true) . "\r\n";
                }
            }
        }
            exit(0);
    }
    public function generate() {
        if(empty($_POST)) {
            $this->display();
        } else {
            $num   = I('num', 0, 'int');
            if(!$num) {
                $this->error('请输入数量！');
            }
            if($num > 10000) {
                $this->error('生成数量超过10000个');
            }
            $datalist = array();
            for($i=0;$i<$num;$i++) {
                $datalist[] = array(
                    'ncode'       => sp_random_string(12)
                );
            }
            if($datalist) {
                M('nuoche_chezhu')->addAll($datalist);
                $this->success('添加成功！', UU('order'));
            }
        }
    }
}